#include <my_header.h>
/*接口名称 gets_client
 * 接口功能简介 用于客户端，接收指定服务器文件到本地
 *返回值的含义
 *return 0: 服务器文件接收成功
 *作者 周利通
 * */
int gets_client(int netfd//通信文件描述符
                ){                                  
    train_t train;
    int filename_length_ret=recv(netfd,&train.length,sizeof(train.length),MSG_WAITALL);
    ERROR_CHECK(filename_length_ret,-1,"recv filename train.length");
    
    int filename_buff_ret=recv(netfd,&train.buff,sizeof(train.buff),MSG_WAITALL);
    ERROR_CHECK(filename_buff_ret,-1,"recv filename train.buff");
    
    char filename[4096]={0};
    memcpy(filename,train.buff,train.length);
    int fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,0666);
    ERROR_CHECK(fd,-1,"open file");
    
    while(1){
        int filedata_length_ret=recv(netfd,&train.length,sizeof(train.length),MSG_WAITALL);
        ERROR_CHECK(filedata_length_ret,-1,"recv filedata train.length");
        
        if(train.length!=1000){
            printf("train.length=%d\n",train.length);
        }
        if(train.length==0){
            break;
        }
        int filedata_buff_ret=recv(netfd,&train.buff,train.length,MSG_WAITALL);
        ERROR_CHECK(filedata_buff_ret,-1,"recv filedata_buff_ret");
        
        int write_ret=write(fd,train.buff,train.length);
        ERROR_CHECK(write_ret,-1,"write");
    }
    return 0;
}

